﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WFDataBindingExercises
{
    public partial class MainForm : Form
    {
        ConCouCitDataClassesDataContext context = new ConCouCitDataClassesDataContext();

        public MainForm()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            this.continentBindingSource.DataSource = context.Continents;
        }

        private void nextCountryButton_Click(object sender, EventArgs e)
        {
            this.countryBindingSource.MoveNext();
        }

        private void previousCountryButton_Click(object sender, EventArgs e)
        {
            this.countryBindingSource.MovePrevious();
        }

        private void continentsListBox_SelectedValueChanged(object sender, EventArgs e)
        {
            object continentId = this.continentsListBox.SelectedValue;
            if (continentId != null)
            {
                this.countryBindingSource.DataSource =
                    from c in context.Countries
                    where c.ContinentId == (int)continentId
                    select c;
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            AddForm addform = new AddForm();
            addform.ShowDialog();
            if (addform.DialogResult == DialogResult.OK)
            {
                int countryId = 
                    (from c in context.Countries
                    where c.Name == this.countryNameTextBox.Text
                    select c.CountryId).FirstOrDefault();
                Town town = new Town
                {
                    Name = addform.TownName,
                    Population = addform.Population,
                    CountryId = countryId
                };
                context.Towns.InsertOnSubmit(town);
            }
        }

        private void reloadButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to reload information?\nAny unsaved changes will be lost.",
                "Reload information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                context = new ConCouCitDataClassesDataContext();
                this.continentBindingSource.DataSource = context.Continents;                
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            context.SubmitChanges();
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if (townsDataGridView.SelectedRows.Count > 0)
            {
                Town town = (Town)townsDataGridView.SelectedRows[0].DataBoundItem;
                EditForm editForm = new EditForm(town.Name, town.Population);
                editForm.Show();
                if (editForm.DialogResult == DialogResult.OK)
                {
                    town.Name = editForm.TownName;
                    town.Population = editForm.Population;
                }
            }
            else
            {
                MessageBox.Show("Please, select a row with town information.", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (townsDataGridView.SelectedRows.Count > 0)
            {
                Town town = (Town)townsDataGridView.SelectedRows[0].DataBoundItem;
                context.Towns.DeleteOnSubmit(town);
            }
            else
            {
                MessageBox.Show("Please, select a row with town information.", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ShowFlag(string countryName)
        {
            switch (countryName)
            {
                case "Bulgaria":
                    flagPictureBox.Image = Properties.Resources.bulgaria;
                    break;
                case "Serbia":
                    flagPictureBox.Image = Properties.Resources.serbia;
                    break;
                case "Russia":
                    flagPictureBox.Image = Properties.Resources.russia;
                    break;
                case "England":
                    flagPictureBox.Image = Properties.Resources.england;
                    break;
            }
        }

        private void countryNameTextBox_TextChanged(object sender, EventArgs e)
        {
            ShowFlag(countryNameTextBox.Text);
        }
    }
}
